/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.LocalDao;
import edu.upc.clase.demo.entity.Local;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
 *
 * @author Jose Vasquez
 */
@Repository
public class LocalDaoImpl extends SimpleJdbcDaoSupport implements  LocalDao 
{
    
    private static Logger log = LoggerFactory.getLogger(LocalDaoImpl.class);
    
     @Autowired
    public LocalDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
    
   @Override
    public Integer insertar(Local local) {

        getJdbcTemplate().update(
                "insert into Local (Nolocal,NoDireccion,CoEncargado,CoUsuario,FlEstado) values (?, ?, ?, ?, ?)",
                local.getCoLocal() ,local.getNoDireccion(),local.getCoEncargado(),local.getCoUsuario(),local.getFlEstado());
        return getSimpleJdbcTemplate().queryForInt("call identity()");
    }
  
    @Override
    public void actualizar(Local local) {
        getJdbcTemplate().update(
                "update local set nolocal = ?, nodireccion = ?, coencargado= ?, cousuario= ?,flestado= ? where colocal = ?",
                local.getNoLocal(),local.getNoDireccion(),local.getCoEncargado(),local.getCoUsuario(),local.getFlEstado(),local.getCoLocal());
    }
     
    @Override
    public void eliminar(Local local) {
        getJdbcTemplate().update(
                "delete from local where CoLocal = ?", local.getCoLocal());
    }

     @Override
    public Local buscar(Integer CoLocal) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select * from local where CoLocal=?",
                    new BeanPropertyRowMapper<Local>(Local.class),CoLocal);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
     @Override
    public List<Local> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "select * from local",
                new BeanPropertyRowMapper<Local>(Local.class));
    }

   @Override
    public List<Local> buscarPorNombre(String nolocal) {
        try {
            Map<String,String> parametros = new HashMap<String,String>();
            parametros.put("nocancha","%"+nolocal+"%");
            return getSimpleJdbcTemplate().query(
                    "select * from local where nolocal like :nolocal",
                    new BeanPropertyRowMapper<Local>(Local.class),parametros);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }  
    
    
}
